<template>
  <van-button
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    v-if="!isFollowed"
    @click="onFollowed"
    :loading="isFollowedLoading"
    >关注</van-button
  >
  <van-button
    class="follow-btn"
    round
    size="small"
    v-else
    @click="onFollowed"
    :loading="isFollowedLoading"
    >已关注</van-button
  >
</template>

<script>
import { deleteUser, followUser } from '@/api/user.js'

export default {
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [String, Number, Object],
      required: true
    }
  },
  data () {
    return { isFollowedLoading: false }
  },
  model: {
    prop: 'isFollowed',
    event: 'updateFollowed'
  },

  created () {},

  methods: {
    async onFollowed () {
      this.isFollowedLoading = true
      try {
        if (this.isFollowed) {
          // 取消关注
          await deleteUser(this.autId)
        } else {
          await followUser(this.autId)
        }
        this.$emit('updateFollowed', !this.isFollowed)
        this.isFollowedLoading = false
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
